#include "interviewcard.h"
#include "ui_interviewcard.h"
#include "debug.h"

InterviewCard::InterviewCard(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::InterviewCard)
{
    ui->setupUi(this);

    init();
}

InterviewCard::~InterviewCard()
{
    delete ui;
}

void InterviewCard::set(const model::InterviewInfo &interviewInfo)
{
    ui->btn_avatar->setIcon(interviewInfo.sender.avatar);
    ui->label_username->setText(interviewInfo.sender.nickname);
    ui->label_title->setText(interviewInfo.title);
    auto content = interviewInfo.content.mid(0, std::min(100ll, interviewInfo.content.size()));
    content.replace('\n', ' ');
    ui->label_content->setText(content + ". . .  点击查看更多");
    ui->label_time->setText(util::formatTime(interviewInfo.timestamp));
}

void InterviewCard::init()
{
    initStyleSheet();
    initPanel();
}

void InterviewCard::initStyleSheet()
{
    this->setStyleSheet(R"(color: #333333;)");
    ui->panel->setStyleSheet(R"(#panel{border-radius: 12px; background-color: #eeeeee; border: 1px solid #DCDFE4;})"); // DCDFE4
#if TEST_UI
    ui->btn_avatar->setStyleSheet(R"(background-color: #ff0000;)");
#endif
}

void InterviewCard::initPanel()
{
    this->setFixedHeight(200); // 80 40 60
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    this->layout()->setContentsMargins(10, 0, 0, 0);
    this->layout()->setSpacing(0);

    ui->panel->layout()->setContentsMargins(20, 0, 20, 0);
    ui->panel->layout()->setSpacing(10);

    ui->head->setFixedHeight(80);
    ui->head->layout()->setContentsMargins(0, 0, 0, 0);
    ui->head->layout()->setSpacing(0);

    ui->btn_avatar->setFixedSize(50, 50);
    ui->head_info->layout()->setContentsMargins(10, 0, 0, 0);

    QFont font_title("微软雅黑", 20, QFont::Bold);
    ui->label_title->setFont(font_title);
    ui->label_title->setFixedHeight(40);
    ui->label_title->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->label_title->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    ui->label_title->setTextFormat(Qt::PlainText);

    QFont font_content("微软雅黑", 10);
    ui->label_content->setFont(font_content);
    ui->label_content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->label_content->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    ui->label_content->setWordWrap(true);
    ui->label_content->setTextFormat(Qt::PlainText);
}

#include <QMouseEvent>
void InterviewCard::mousePressEvent(QMouseEvent *event)
{
    event->accept();
}

void InterviewCard::mouseReleaseEvent(QMouseEvent *event)
{
    emit clicked();
    event->accept();
}

void InterviewCard::mouseMoveEvent(QMouseEvent *event)
{
    event->accept();
}
